package com.foriseholdings.write2mysql.action;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import com.foriseholdings.commons.utils.PropertyUtil;

/*
 * 每日清空数据库表tb_recommend_sort
 * 后期版本应去掉清空功能，
 * target :动态解析数据,存储数据,更新数据记录
 */
public class ClearDataTable {

	private static String jdbcDri = PropertyUtil.getProperty("jdbcDri");
	private static String username = PropertyUtil.getProperty("username");
	private static String password = PropertyUtil.getProperty("password");

	// public static void main(String[] args) {
	// clearTb();
	// }

	public static boolean clearTb(String busi_code) {

		Connection conn = null;
		Statement st = null;

		try {
			// 注册驱动(数据库的类型)
			Class.forName("com.mysql.jdbc.Driver");
			// 获取连接
			conn = DriverManager.getConnection(jdbcDri, username, password);
			// 得到运行环境
			st = conn.createStatement();
			// 执行SQL
			String sql = "delete from elep_recommended_sort where bus_code =" + "'" + busi_code + "'";
			// System.out.println("sql=" + sql);
			int result = st.executeUpdate(sql);

			// 处理结果
			if (result > 0) {
				System.out.println("刪除操作成功");
				return true;
			} else {
				System.out.println("刪除操作失败");
				return false;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			// 释放资源
			if (st != null) {
				try {
					st.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					st = null;// --> 让他迅速成为java gc的对象
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					conn = null;// --> 让他迅速成为java gc的对象
				}
			}
		}
		return false;
	}

	public static boolean updateBusCode(String buss_code, String algorithm_type, String date) {

		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			// 注册驱动(数据库的类型)
			Class.forName("com.mysql.jdbc.Driver");
			// 获取连接
			conn = DriverManager.getConnection(jdbcDri, username, password);
			// 得到运行环境
			// pstmt = conn.createStatement();
			String sql  = "update elep_recommended_sort set bus_code ="  +  "'"  +buss_code+ "' where algorithm_date =" +"'"+date+"'" +"AND algorithm_type = "+"'"+ algorithm_type+"'" ;
			pstmt = (PreparedStatement) conn.prepareStatement(sql);
			// 执行SQL
			// System.out.println("sql=" + sql);

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			// 释放资源
			if (pstmt != null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					pstmt = null;// --> 让他迅速成为java gc的对象
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					conn = null;// --> 让他迅速成为java gc的对象
				}
			}
		}
		return false;
	}

}